/**
 * 
 */
package au.edu.swin.videoshop.ejb.bean;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import au.edu.swin.videoshop.bean.Title;
import au.edu.swin.videoshop.bean.TitleCategory;
import au.edu.swin.videoshop.bean.TitleCopy;
import au.edu.swin.videoshop.bean.TitleSearchCriteria;
import au.edu.swin.videoshop.persistence.TitleCategoryEntity;
import au.edu.swin.videoshop.persistence.TitleCopyEntity;
import au.edu.swin.videoshop.persistence.TitleEntity;
import au.edu.swin.videoshop.service.TitleService;

/**
 * @author julie
 *
 */
@Stateless
public class TitleServiceBean implements TitleService{
	
	@PersistenceContext
	private EntityManager em;

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#addTitle(au.edu.swin.videoshop.bean.Title)
	 */
	public void addTitle(Title title) {
		TransferService service = new TransferService();
		TitleEntity titleEntity = service.toTitleEntity(title);
		em.persist(titleEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#addTitleCategory(au.edu.swin.videoshop.bean.TitleCategory)
	 */
	public void addTitleCategory(TitleCategory titleCategory) {
		TransferService service = new TransferService();
		TitleCategoryEntity titleCategoryEntity = service.toTitleCategoryEntity(titleCategory);
		em.persist(titleCategoryEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#updateTitle(au.edu.swin.videoshop.bean.Title)
	 */
	public void updateTitle(Title title) {
		TransferService service = new TransferService();
		TitleEntity titleEntity = service.toTitleEntity(title);
		em.merge(titleEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#updateTitleCategory(au.edu.swin.videoshop.bean.TitleCategory)
	 */
	public void updateTitleCategory(TitleCategory titleCategory) {
		TransferService service = new TransferService();
		TitleCategoryEntity titleCategoryEntity = service.toTitleCategoryEntity(titleCategory);
		em.merge(titleCategoryEntity);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#markDeleteTitleCategory(long)
	 */
	public void markDeleteTitleCategory(long id) {
		TitleCategoryEntity titleCategoryEntity = em.find(TitleCategoryEntity.class, id);
		if (titleCategoryEntity != null) {
			titleCategoryEntity.setDeleted(true);
			em.merge(titleCategoryEntity);
		}
	}
	
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#markDeleteTitle(long)
	 */
	public void markDeleteTitle(long id) {
		TitleEntity titleEntity = em.find(TitleEntity.class, id);
		if (titleEntity != null) {
			titleEntity.setDeleted(true);
			em.merge(titleEntity);
		}
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#markDeleteTitleCopy(java.lang.String)
	 */
	public void markDeleteTitleCopy(String id) {
		TitleCopyEntity titleCopyEntity = em.find(TitleCopyEntity.class, id);
		if (titleCopyEntity != null) {
			titleCopyEntity.setDeleted(true);
			em.merge(titleCopyEntity);
		}
	}
	

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findTitleCopyByTilteCopyId(java.lang.String)
	 */
	public TitleCopy findTitleCopyByTilteCopyId(String id) {
		TitleCopyEntity titleCopyEntity = em.find(TitleCopyEntity.class, id);
		if (titleCopyEntity != null) {
			TransferService service = new TransferService();
			return service.toTitleCopyTransferObject(titleCopyEntity);
		}
		else {
			return null;
		}
	}
	

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findCategoryByTilteCategoryId(long)
	 */
	public TitleCategory findCategoryByTilteCategoryId(long id) {
		TitleCategoryEntity categoryEntity = em.find(TitleCategoryEntity.class, id);
		if (categoryEntity != null) {
			TransferService service = new TransferService();
			return service.toTitleCategoryTransferObject(categoryEntity);
		}
		else {
			return null;
		}
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findAllCategories()
	 */
	public List<TitleCategory> findAllCategories() {
		Query query = em.createNamedQuery("findAllTitleCategories");
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toTitleCategoryTransferObjectList(entityList);
	}
	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findAllTitleCopies()
	 */
	public List<TitleCopy> findAllTitleCopies() {
		Query query = em.createNamedQuery("findAllTitleCopies");
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toTitleCopyTransferObjectList(entityList);
	}
	

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findAllTitles()
	 */
	public List<Title> findAllTitles() {
		Query query = em.createNamedQuery("findAllTitles");
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toTitleTransferObjectList(entityList);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findTitleCopiesByStoreId(long)
	 */
	public List<TitleCopy> findTitleCopiesByStoreId(long storeId) {
		Query query = em.createNamedQuery("findTitleCopiesByStoreId");
		query.setParameter("storeId", storeId);
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toTitleCopyTransferObjectList(entityList);
	}

	
	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findTitlesByCategoryId(long)
	 */
	public List<Title> findTitlesByCategoryId(long categoryId) {
		Query query = em.createNamedQuery("findTitlesByCategoryId");
		query.setParameter("titleCategoryId", categoryId);
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toTitleTransferObjectList(entityList);
	}

	/* (non-Javadoc)
	 * @see au.edu.swin.videoshop.service.TitleService#findTitlesBySearchCriteria(au.edu.swin.videoshop.bean.TitleSearchCriteria)
	 */
	public List<Title> findTitlesBySearchCriteria(TitleSearchCriteria criteria) {
		Query query = null;
		if (criteria.getCategoryId() == null) {
			query = em.createNamedQuery("findTitlesInAnyCategory");
		}
		else {
			query = em.createNamedQuery("findTitles");
			query.setParameter("titleCategoryId", criteria.getCategoryId());
		}
		if (criteria.getTitleName() == null) {
			query.setParameter("titleName", "%");
		}
		else {
			query.setParameter("titleName", "%" + criteria.getTitleName().toLowerCase() + "%");
		}
		if (criteria.getClassification() == null) {
			query.setParameter("classification", "%");
		}
		else {
			query.setParameter("classification", criteria.getClassification());
		}
		List entityList = query.getResultList();
		TransferService service = new TransferService();
		return service.toTitleTransferObjectList(entityList);
	}




}
